//
//  SCTPetMarketListCell.m
//  SuperCat
//
//  Created by QG on 2020/3/28.
//  Copyright © 2020 和信金谷. All rights reserved.
//

#import "SCTPetMarketCell.h"
#import <SDCycleScrollView/SDCycleScrollView.h>

#define imgUrl @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1585380621300&di=69afbcdbe8341f49108aead37e4abe8e&imgtype=0&src=http%3A%2F%2Fdik.img.kttpdq.com%2Fpic%2F160%2F111422%2F6474bdadb88aea64_1680x1050.jpg"
#define title @"我是一棵葱, 站在风雨中, 谁敢拿我沾大酱, 我Ri他老祖宗"

// - MARK: <-- 首页列表的 cell -->
NSString *kSCTPetMarketListCellID = @"SCTPetMarketListCellID";

@interface SCTPetMarketListCell ()

/** 猫图片 */
@property(nonatomic, weak)UIImageView *iconImageView;

/** 猫名字 */
@property(nonatomic, weak)UILabel *nameLabel;

/** 猫时间 */
@property(nonatomic, weak)UILabel *timeLabel;

/** 猫价格 */
@property(nonatomic, weak)UILabel *priceLabel;

/** 猫预约 */
@property(nonatomic, weak)UILabel *bespeakLabel;

/** 猫收益 */
@property(nonatomic, weak)UILabel *incomeLabel;

/** 猫积分 */
@property(nonatomic, weak)UILabel *integralLabel;

/** 预约买猫 */
@property(nonatomic, weak)UIButton *bespeakButton;

@end

@implementation SCTPetMarketListCell{
    CGFloat _imgW;
    CGFloat _padding;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    SCTPetMarketListCell *cell = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    _imgW = 70;
    _padding = 12;
    [self setupUI];
    return cell;
}

/** 设置 UI */
-(void)setupUI{
    CGFloat viewW = (self.w - _imgW - _padding * 2) / 2;
    
    /** 猫图片 */
    UIImageView *iconImageView = ({
        UIImageView *imgV = ImageView.addTo(self.contentView);
        _iconImageView = imgV;
        [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(_imgW, _imgW));
            make.left.equalTo(self.contentView).offset(_padding);
            make.top.equalTo(self.contentView).offset(_padding);
        }];
        imgV;
    });

    /** 猫名字 */
    UILabel *nameLabel = ({
        UILabel *label = Label.str(title).fnt(12).color(@"#000000").centerAlignment.addTo(self.contentView);
        label.backgroundColor = [UIColor greenColor];
        _nameLabel = label;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(iconImageView);
            make.top.equalTo(iconImageView.mas_bottom).offset(_padding);
            make.bottom.equalTo(self.contentView).offset(-_padding);
        }];
        label;
    });

    /** 猫时间 */
    UILabel * timeLabel = ({
        UILabel *label = Label.str(title).fnt(12).color(@"#000000").addTo(self.contentView);
        _timeLabel = label;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconImageView.mas_right).offset(_padding);
            make.top.equalTo(iconImageView);
            make.width.mas_equalTo(viewW);
        }];
        label;
    });

    /** 猫预约 */
    UILabel *bespeakLabel = ({
        UILabel *label = Label.str(title).fnt(12).color(@"#000000").addTo(self.contentView);
           _bespeakLabel = label;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView);
            make.left.equalTo(timeLabel.mas_right).offset(_padding);
            make.top.equalTo(timeLabel);
        }];
           label;
       });


    /** 猫价格 */
    UILabel *priceLabel = ({
        UILabel *label = Label.str(title).fnt(12).color(@"#000000").addTo(self.contentView);
        _priceLabel = label;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(timeLabel);
            make.top.equalTo(timeLabel.mas_bottom).offset(_padding);
        }];
        label;
       });


    /** 猫收益 */
    UILabel *incomeLabel = ({
           UILabel *label = Label.str(title).fnt(12).color(@"#000000").addTo(self.contentView);
           _incomeLabel = label;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.left.equalTo(bespeakLabel);
            make.top.equalTo(priceLabel);
            make.width.mas_equalTo(viewW);
        }];
           label;
       });
    
    /** 猫积分格 */
    UILabel *integralLabel = ({
        UILabel *label = Label.str(title).fnt(12).color(@"#000000").addTo(self.contentView);
        _integralLabel = label;
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(timeLabel);
            make.top.equalTo(priceLabel.mas_bottom).offset(_padding);
        }];
        label;
    });

    /** 预约买猫 */
    UIButton *bespeakButton = ({
        CGFloat hei = 30;
        UIButton *button = Button.bgColor(@"#557799").addTo(self.contentView);
        [button setTitle:@"预约" forState:UIControlStateNormal];
        [button setTitle:@"不可预约" forState:UIControlStateDisabled];
        button.clipsToBounds = YES;
        button.layer.cornerRadius = hei / 2;
        [button addTarget:self action:@selector(onBespeakButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _bespeakButton = button;
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(hei);
            make.centerY.equalTo(integralLabel);
            make.centerX.equalTo(incomeLabel);
        }];
        button;
   });
}

/** 预约按钮的点击 */
-(void)onBespeakButtonClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectListModel:)]) {
        [self.delegate didSelectListModel:self.listModel];
    }
}

/** 设置 model */
-(void)setListModel:(SCTPetMarketListModel *)listModel{
    _listModel = listModel;
    CGFloat _imgW = 70;
    CGFloat _padding = 12;

    [self.iconImageView setImageWithUrlStr:imgUrl];
    self.nameLabel.text = title;
    self.timeLabel.text = @"时间 : ".a(title);
    self.priceLabel.text = @"价值 : ".a(title);
    self.bespeakLabel.text = @"预约/即抢 : ".a(title);
    self.incomeLabel.text = @"合约收益 : ".a(title);
    self.integralLabel.text = @"奖励积分 : ".a(title);
    self.bespeakButton.enabled = YES;
    
    listModel.cellHei = _imgW + _padding * 4;
}

@end

// - MARK: <-- 首页banner的 cell -->
@interface SCTPetMarketBannerView () <SDCycleScrollViewDelegate>

/** 无限轮播图 */
@property(nonatomic, weak)SDCycleScrollView *sliderView;

@end

@implementation SCTPetMarketBannerView{
    CGFloat _timeLableHei;
}

-(void)setupViews{
    _timeLableHei = 30;
}

/** 无限轮播图 */
- (SDCycleScrollView *)sliderView{
    if (!_sliderView) {
        SDCycleScrollView *sliderView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - _timeLableHei) delegate:self placeholderImage:nil];
        sliderView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        [self addSubview:sliderView];
        self.sliderView = sliderView;
    }
    return _sliderView;
}

/** 时间的label */
- (UILabel *)timeLabel{
    if (!_timeLabel) {
        UIView *timeView = View.addTo(self);
        [timeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.mas_equalTo(_timeLableHei);
        }];
        
        // - 系统消息
        UILabel *msgLabel = Label.leftAlignment.str(@"系统消息 : ").fnt(@12).color(@"#000000").addTo(timeView);
        [msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(timeView);
            make.width.mas_equalTo(100);
        }];
        
        // - 分割线
        UIView *sepView = View.bgColor(@"#666666").addTo(timeView);
        [sepView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(msgLabel);
            make.width.mas_equalTo(1);
            make.left.equalTo(msgLabel.mas_right);
        }];
        
        // - 时间
        UILabel *timeLabel = Label.leftAlignment.rightAlignment.fnt(@12).color(@"#000000").addTo(timeView);
        [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(timeView);
        }];
        _timeLabel = timeLabel;
    }
    return _timeLabel;
}

/** 轮播图的数组 */
-(void)setBannerModelsArray:(NSArray<SCTPetMarketBannerModel *> *)bannerModelsArray{
    _bannerModelsArray = bannerModelsArray;
    NSMutableArray *imageUrls = [NSMutableArray array];
    NSMutableArray *titles = [NSMutableArray array];

    for (SCTPetMarketBannerModel* model in bannerModelsArray) {
        [imageUrls addObject:imgUrl];
        [titles addObject:title];
    }
    self.sliderView.imageURLStringsGroup = imageUrls;
    self.sliderView.titlesGroup = titles;
}

/** 代理方法 */
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    SCTPetMarketBannerModel *bannerModel = self.bannerModelsArray[index];
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectBannderModel:)]) {
        [self.delegate didSelectBannderModel:bannerModel];
    }
}



+(CGFloat)viweHei{
    return 250;
}
@end

